package LanQiao._14JavaB;

import java.util.Scanner;

public class _5合并区域 {

    //WA：
    //ps：存在A区域和B区域合并时导致A区域原本不联通的路径通过B区域联通了
    //正确方法好像靠模拟＋暴力

    static int count = 0;
    static boolean flag = false;
    static int maxA1 = 0, maxA2 = 0;
    static int maxB1 = 0, maxB2 = 0;
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n = scan.nextInt();
        int[][] arr1 = new int[n][n];
        int[][] arr2 = new int[n][n];
        boolean[][] visited1 = new boolean[n][n];
        boolean[][] visited2 = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr1[i][j] = scan.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr2[i][j] = scan.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr1[i][j] == 1) {
                    dfs(arr1,i,j,visited1);
                    maxA1 = Math.max(maxA1, count);
                    if (flag) maxA2 = Math.max(maxA2, count);
                    flag = false;
                    count = 0;
                }
            }
        }
        flag = false;
        count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr1[i][j] == 1) {
                    dfs(arr2,i,j,visited2);
                    maxB1 = Math.max(maxB1, count);
                    if (flag) maxB2 = Math.max(maxB2, count);
                    flag = false;
                    count = 0;
                }
            }
        }
        if (maxA2 == 0 || maxB2 == 0){
            System.out.println(Math.max(maxA1, maxB1));
        }else {
            int max = 0;
            max = Math.max(maxA2 + maxB2, maxA1);
            max = Math.max(max, maxB1);
            System.out.println(max);
        }


        scan.close();

    }

    public static void dfs(int[][] arr,int i,int j,boolean[][] visited){
        if (i == -1 || i == arr.length || j == -1 || j == arr[i].length || arr[i][j] == 0 || visited[i][j]){
            return;
        }
        if (i == arr.length - 1 || j == arr[i].length - 1 || i == 0 || j == 0){
            flag = true;
        }
        visited[i][j] = true;
        count++;
        int[][] arg = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
        for (int[] ints : arg) {
            int x = ints[0], y = ints[1];
            dfs(arr, i + x,j + y,visited);
        }
    }
}
